package com.pic4493.app;

import java.util.ArrayList;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.pic4493.base.BaseUI;
import com.pic4493.comm.PubConst;
import com.pic4493.comm.Session;
import com.pic4493.entities.ESubscribe;
import com.pic4493.updater.TaskCheckVersion;
import com.pic4493.utils.UtiDialog;
import com.pic4493.utils.UtiFile;

public class UISplash2 extends BaseUI
{
   @Override
   protected void onDestroy()
   {
	  super.onDestroy();
   }

   private LayoutInflater mInflater;
   // 滑动壁纸
   private RelativeLayout mLayWallPaper;
   private ImageView mImgWallPaper;
   private ImageView mImgDownload;

   private ArrayList<ESubscribe> mList;

   private boolean isSave = false;

   private float o_X, o_Y, n_X, n_Y;

   private void initView()
   {
	  try
	  {
		 mLayWallPaper = (RelativeLayout) findViewById(R.id.uisplash2_main);
		 mLayWallPaper.setBackgroundDrawable(new BitmapDrawable(Session.getInstance().picCache));
		 mImgDownload = (ImageView) findViewById(R.id.uisplash2_img_download);
	  }
	  catch (Exception e)
	  {
		 Log.d("ERROR", "UISplash2_initView() " + e);
	  }

	  Animation animation = AnimationUtils.loadAnimation(mContext, R.anim.zoom_small);
	  mLayWallPaper.startAnimation(animation);
	  animation.setAnimationListener(new Animation.AnimationListener()
	  {
		 @Override
		 public void onAnimationStart(Animation animation)
		 {
		 }

		 @Override
		 public void onAnimationRepeat(Animation animation)
		 {
		 }

		 @Override
		 public void onAnimationEnd(Animation animation)
		 {
			// 检查更新
			TaskCheckVersion task = new TaskCheckVersion(mContext);
			task.execute();
			mLayWallPaper.setOnTouchListener(new View.OnTouchListener()
			{
			   @Override
			   public boolean onTouch(View v, MotionEvent event)
			   {
				  final int action = event.getAction();
				  DisplayMetrics dm = new DisplayMetrics();
				  getWindowManager().getDefaultDisplay().getMetrics(dm);
				  int w = dm.widthPixels;
				  int h = dm.heightPixels;
				  switch (action)
				  {
					 case MotionEvent.ACTION_DOWN:
						o_X = event.getX();
						o_Y = event.getY();
						break;
					 case MotionEvent.ACTION_MOVE:
						n_X = event.getX() - o_X;
						n_Y = event.getY() - o_Y;
						// if (n_Y > 0)//往上滑动图片
						// {
						// mLayWallPaper.layout((int) n_X, (int) n_Y, (int) n_X
						// + w, (int) n_Y + h);
						// mLayWallPaper.invalidate();
						// }
						break;
					 case MotionEvent.ACTION_UP:
						if (n_Y < -100)
						{
						   Intent intent = new Intent();
						   intent.putParcelableArrayListExtra(PubConst.Intent_Key.Subscribes, mList);
						   intent.setClass(mContext, UIMain.class);
						   intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
						   setResult(RESULT_OK);
						   startActivity(intent);
						   finish();
						   overridePendingTransition(R.anim.slide_in_from_bottom, R.anim.slide_out_to_top);
						}
						else
						{
						   n_X = 0;
						   n_Y = 0;
						   mLayWallPaper.layout((int) 0, (int) 0, w, h);
						   mLayWallPaper.invalidate();
						}
						break;
					 default:
						break;
				  }
				  return false;
			   }
			});
		 }
	  });
   }

   private void initEvent()
   {
	  mImgDownload.setOnClickListener(new View.OnClickListener()
	  {
		 @Override
		 public void onClick(View v)
		 {
			try
			{
			   if (!isSave)
			   {
				  UtiFile.saveMyBitmap(mContext, Session.getInstance().picCache);
				  isSave = true;
			   }
			   else
			   {
				  UtiDialog.getInstance().ShowToastInfoShort(mContext, "您已经保存过该图片");
			   }
			}
			catch (Exception e)
			{
			   Log.d("ERROR", "UIWallPaper.initEvent().new OnClickListener() {...}_onClick(v) " + e);
			}
		 }
	  });
   }

   @Override
   protected void onCreate(Bundle savedInstanceState)
   {
	  super.onCreate(savedInstanceState);
	  setContentView(R.layout.uisplash2);
	  mList = getIntent().getParcelableArrayListExtra(PubConst.Intent_Key.Subscribes);
	  initView();
	  initEvent();
   }

   @Override
   public boolean onKeyDown(int keyCode, KeyEvent event)
   {
	  if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0)
	  { // 按下的如果是BACK，同时没有重复
		 FinishDialog();
	  }
	  return false;
   }

   public void FinishDialog()
   {
	  DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener()
	  {
		 @Override
		 public void onClick(DialogInterface dialog, int which)
		 {
			dialog.dismiss();
			System.exit(0);
		 }
	  };
	  UtiDialog.getInstance().ShowOKCancelAlertDialog(mContext, "提示", "您打算退出程序吗？", listener);
   }
}
